package com.atmaowei.service.impl;

import com.atmaowei.controller.request.TeacherPageRequest;
import com.atmaowei.entity.Course;
import com.atmaowei.entity.Department;
import com.atmaowei.entity.Teacher;
import com.atmaowei.mapper.TeacherMapper;
import com.atmaowei.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void save(Teacher teacher) {
        teacherMapper.save(teacher);
    }

    @Override
    public String getCnoByCname(String cname) {
        return teacherMapper.getCnoByCname(cname);
    }

    @Override
    public String getTnameByTno(String tno) {
        return teacherMapper.getTnameByTno(tno);
    }

    @Override
    public Course getCnameByCno(String cno) {
        return teacherMapper.getCnameByCno(cno);
    }


    @Override
    public PageInfo<Teacher> page(TeacherPageRequest teacherPageRequest) {
        PageHelper.startPage(teacherPageRequest.getPageNum(), teacherPageRequest.getPageSize());
        List<Teacher> list = teacherMapper.page(teacherPageRequest);
        for (Teacher teacher : list) {
            teacher.setCno1name(getCnameByCno(teacher.getCno1()).getCname());
            teacher.setCno2name(getCnameByCno(teacher.getCno2()).getCname());
            teacher.setCno3name(getCnameByCno(teacher.getCno3()).getCname());
        }
        PageInfo<Teacher> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void delete(String tno) {
        teacherMapper.delete(tno);
    }

    @Override
    public void update(Teacher teacher) {
        teacherMapper.update(teacher);
    }

    @Override
    public Teacher getByTno(String tno) {
        Teacher teacher = teacherMapper.getByTno(tno);
        teacher.setCno1name(getCnameByCno(teacher.getCno1()).getCname());
        teacher.setCno2name(getCnameByCno(teacher.getCno2()).getCname());
        teacher.setCno3name(getCnameByCno(teacher.getCno3()).getCname());
        return teacher;
    }

    @Override
    public List<Course> getAllCourse() {
        return teacherMapper.getAllCourse();
    }

    @Override
    public void deleteSelected(List<Teacher> teachers) {
        for (Teacher teacher : teachers) {
            teacherMapper.deleteSelected(teacher.getTno());
        }
    }

    @Override
    public void statusChange(String tno) {
        Teacher teacher = teacherMapper.getByTno(tno);
        String status = null;
        if (teacher.getStatus().equals("1")) {
            status = "0";
            teacherMapper.statusChange(tno,status);
        } else {
            status = "1";
            teacherMapper.statusChange(tno,status);
        }
    }
}
